package com.it.controller;


import com.it.domain.BlogTypeEntity;
import com.it.service.BlogService;
import com.it.service.BlogTypeService;
import com.it.vo.BlogQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.List;

/**
 * @author : code1997
 * @date : 2021/9/9 22:46
 */
@Controller
public class TypeShowController {

    @Autowired
    private BlogTypeService typeService;
    @Autowired
    private BlogService blogService;

    //@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
    @GetMapping({"types/{id}"})
    public String helloController2(@PathVariable Long id, Model model) {
        //相当于查询全表
        List<BlogTypeEntity> blogTypeEntities = typeService.listTypeTop(10000);
        if (id == -1) {
            BlogTypeEntity blogTypeEntity = blogTypeEntities.get(0);
            /*避免没有类型导致出现空指针异常的现象*/
            if (blogTypeEntity != null) {
                id = blogTypeEntity.getId();
            }
        }
        BlogQueryVo blogQueryVo = new BlogQueryVo();
        blogQueryVo.setTypeId(id);
        model.addAttribute("types", blogTypeEntities);
        model.addAttribute("page", blogService.listBlog(new HashMap<>(), blogQueryVo));
        model.addAttribute("activeTypeId", id);
        return "types";
    }

}
